<?php
if(defined('IZ_ACCNAME')) {
   if($_GET['debug'] == '0') {
     $_SESSION['debug'] = 0;
   }
   echo '<div id="errorbox2">
   <p>Variables could not be initialised, try deleting settings.php from <em>includes&#47;data</em>, then reinstalling iZeit.<br />For support, please copy and paste the information below and post in the <a href="http://forums.izeit.nu/index.php?showforum=7">iZeit Support Forums</a>.</p>
   <p><strong>Debugging information</strong><br />
      Install path&#58; '.$_SESSION['IZ_CALPATH'].'<br />
      Mode&#58; '.$_SESSION['IZ_CALMODE'].'<br />
      language&#58; '.$_SESSION['IZ_LANG'].'<br />
      URL&#58; '.$_SERVER['SCRIPT_URI'].'<br />
      Server Info&#58; '.$_SERVER['SERVER_SOFTWARE'].'<br />
      File path&#58; '.$_SERVER['SCRIPT_FILENAME'].'<br />
      PHP version&#58; '.phpversion().' &#40;Looks like ';
       if(version_compare(phpversion(),"5",">")) {
         echo 'greater than';
       } else {
         echo 'less than';
       }
 echo ' version 5&#41;<br />
   </p><div style="text-align: left">';
   if(IZ_CALMODE == 'mysql') {
     if($_SESSION['MYSQLi']) {
       global $dblink;
       $dblink = mysql_connect($dbhost, $dbuser , $dbpass);
       mysql_select_db($dbdatabase, $dblink) or $error .= mysql_error().'<br />';            
       echo '<p><strong>MySQL settings</strong><br />'."\n".'
       MySQL Version&#58; '.mysql_get_server_info().' &#40;Looks like ';
         if(version_compare((string)$sqlver,"4.1",">=")) {
           echo 'greater than';
         } else {
           echo 'less than';
         }
       echo ' version 4.1&#41;<br />';
       $tablelist = mysql_query("SHOW TABLES FROM $dbdatabase") or $error .= mysql_error().'<br />';
       echo '<strong>'.mysql_num_rows($tablelist).' tables on database&#58; </strong>'.$dbdatabase.'<br />';           
       while($tablename = mysql_fetch_row($tablelist)) {
         echo '<strong>&#45;</strong>'.$tablename[0].' &#40;';
         $fields = mysql_query("SHOW COLUMNS FROM `$tablename[0]`") or $error .= mysql_error().'<br />';
         $cols = array();
         $numrows = @mysql_num_rows($fields) or $error .= mysql_error().'<br />';
         if($numrows > 0) {
           while ($curfields = mysql_fetch_array($fields)) {
             $cols[] = $curfields[0];
           }
           echo implode(", ",$cols);
         } else {
           echo 'No fields';
         }
         echo '&#41;<br />'."\n";
         $vals = mysql_query("SELECT * FROM `$tablename[0]`") or $error .= mysql_error().'<br />';
         $numcols = count($cols);           
         if(is_resource($vals)) {
           while($settingrow = mysql_fetch_object($vals)) {
             echo '<strong>&#45;&#45;&#45;'.$settingrow->$cols[0].'</strong><br />'."\n";
             for($c = 1;$c<$numcols;$c++) {          
               echo '&#45;&#45;&#45;&#45;&#45;&#45;'.$settingrow->$cols[$c].'<br />';
             }
           }         
         }  
       }
     } else {
       $dblink = mysql_connect($dbhost, $dbuser , $dbpass);
       mysql_select_db($dbdatabase, $dblink) or $error .= mysql_error().'<br />';            
       echo '<p><strong>MySQL settings</strong><br />'."\n".'
       MySQL Version&#58; '.mysql_get_server_info().' &#40;Looks like ';
         if(version_compare((string)$sqlver,"4.1",">=")) {
           echo 'greater than';
         } else {
           echo 'less than';
         }
       echo ' version 4.1&#41;<br />';
       $tablelist = mysql_query("SHOW TABLES FROM $dbdatabase") or $error .= mysql_error().'<br />';
       echo '<strong>'.mysql_num_rows($tablelist).' tables on database&#58; </strong>'.$dbdatabase.'<br />';           
       while($tablename = mysql_fetch_row($tablelist)) {
         echo '<strong>&#45;</strong>'.$tablename[0].' &#40;';
         $fields = mysql_query("SHOW COLUMNS FROM `$tablename[0]`") or $error .= mysql_error().'<br />';
         $cols = array();
         $numrows = @mysql_num_rows($fields) or $error .= mysql_error().'<br />';
         if($numrows > 0) {
           while ($curfields = mysql_fetch_array($fields)) {
             $cols[] = $curfields[0];
           }
           echo implode(", ",$cols);
         } else {
           echo 'No fields';
         }
         echo '&#41;<br />'."\n";
         $vals = mysql_query("SELECT * FROM `$tablename[0]`") or $error .= mysql_error().'<br />';
         $numcols = count($cols);           
         if(is_resource($vals)) {
           while($settingrow = mysql_fetch_object($vals)) {
             echo '<strong>&#45;&#45;&#45;'.$settingrow->$cols[0].'</strong><br />'."\n";
             for($c = 1;$c<$numcols;$c++) {          
               echo '&#45;&#45;&#45;&#45;&#45;&#45;'.$settingrow->$cols[$c].'<br />';
             }
           }         
         }  
       }
     }
   } elseif(IZ_CALMODE == 'postgre') {
     pg_connect("host=$dbhost dbname=$dbdatabase user=$dbuser password=$dbpass");
     echo '<p><strong>PostgreSQL settings</strong><br />'."\n";
     $pgver = pg_version();
     echo 'PostgreSQL Version&#58; '.$pgver['client'].'<br />';
     $tablelist = pg_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'Public'") or $error .= pg_last_error().'<br />';
     echo '<strong>'.pg_num_rows($tablelist).' tables on database&#58; </strong>'.$dbdatabase.'<br />';           
     while($tablename = pg_fetch_row($tablelist)) {
       echo '<strong>&#45;</strong>'.$tablename[0].' &#40;';
       $fields = pg_query("SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '$tablename[0]'") or $error .= pg_last_error().'<br />';
       $cols = array();
       $numrows = @pg_num_rows($fields) or $error .= pg_last_error().'<br />';
       if($numrows > 0) {
         while($curfields = pg_fetch_array($fields)) {
//var_dump($curfields);
           $cols[] = $curfields['column_name'];
         }
         echo implode(", ",$cols);
       } else {
         echo 'No fields';
       }
       echo '&#41;<br />'."\n";
       $vals = pg_query("SELECT * FROM \"$tablename[0]\"") or $error .= pg_last_error().'<br />';
       $numcols = count($cols);           
       if(is_resource($vals)) {
         while($settingrow = pg_fetch_object($vals)) {
           echo '<strong>&#45;&#45;&#45;'.$settingrow->$cols[0].'</strong><br />'."\n";
           for($c = 1;$c<$numcols;$c++) {          
             echo '&#45;&#45;&#45;&#45;&#45;&#45;'.$settingrow->$cols[$c].'<br />';
           }
         }         
       }  
     }         
   } elseif(IZ_CALMODE == 'msq') {
     $dblink = mssql_connect($dbhost, $dbuser, $dbpass);
     mssql_select_db($dbdatabase, $dblink) or die('&#35;44&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mssql_get_last_message());
     echo '<p><strong>MSSQL settings</strong><br />'."\n";
     $tablelist = mssql_query("SELECT * FROM INFORMATION_SCHEMA.TABLES") or $error .= mssql_get_last_message().'<br />';
     echo '<strong>'.mssql_num_rows($tablelist).' tables on database&#58; </strong>'.$dbdatabase.'<br />';           
     while($tablename = mssql_fetch_row($tablelist)) {
       echo '<strong>&#45;</strong>'.$tablename[2].' &#40;';
       $fields = mssql_query("SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '$tablename[2]'") or $error .= mssql_get_last_message().'<br />';
       $cols = array();
       $numrows = @mssql_num_rows($fields) or $error .= mssql_get_last_message().'<br />';
       if($numrows > 0) {
         while ($curfields = mssql_fetch_array($fields)) {
           $cols[] = $curfields[3];
         }
         echo implode(", ",$cols);
       } else {
         echo 'No fields';
       }
       echo '&#41;<br />'."\n";
       $vals = mssql_query("SELECT * FROM [$tablename[2]]") or $error .= mssql_get_last_message().'<br />';
       $numcols = count($cols);           
       if(is_resource($vals)) {
         while($settingrow = mssql_fetch_object($vals)) {
           echo '<strong>&#45;&#45;&#45;'.$settingrow->$cols[0].'</strong><br />'."\n";
           for($c = 1;$c<$numcols;$c++) {          
             echo '&#45;&#45;&#45;&#45;&#45;&#45;'.$settingrow->$cols[$c].'<br />';
           }
         }         
       }  
     }
   }
   echo '<br /><strong>Errors</strong><br /> '.$error.'</p></div></div>';
} else {
  // Directly accessed
  header('Location: ../');
} ?>